<?php
namespace Infinity;

class FieldRow {
    private $items;
    private $entity;
    private $field;
    private $entity_type;

    public function __construct($items, $entity, $entity_type, $field) {
        $this->items = $items;
        $this->entity = $entity;
        $this->field = $field;
        $this->entity_type = $entity_type;
    }
    
    function __get($column)
    {
        return $this->get($column);
    }
    
    function __set($column, $value)
    {
        return $this->set($column, $value);
    }

    public function get($column)
    {
        if(count($this->items) == 0) {
            throw new \Exception('FieldRow: items is empty. field:'.$this->field.' column:'.$column);
        }
        if(count($this->items) == 1) {
            if(isset($this->items[0][$column]))
                return $this->items[0][$column];
            else {
                $this->printBacktrace();
                throw new \Exception('FieldRow: column "'.$column.'" is empty. field: '.$this->field);
            }
        } else {
            return array_column($this->items, $column);
        }
    }
  
    public function set($column, $value)
    {
        if(is_array($value)) {
            $this->items = array();
            foreach($value as $key => $val) {
                $this->items[$key][$column] = $val;
            }
        } else {
            $this->items[0][$column] = $value;
        }
        $this->entity->{$this->field}[LANGUAGE_NONE] = $this->items;
        return $this;
    }

    public function getItems() {
        return $this->items;
    }

    public function label() {
        $field_info = field_info_instance($this->entity_type, $this->field, $this->entity->type);
        if(!empty($field_info['label'])) return $field_info['label'];
        return '';
    }

    public function type() {
        $field_info = field_info_instance($this->entity_type, $this->field, $this->entity->type);
        if(!empty($field_info['widget']['type']))
            return $field_info['widget']['type'];
        return '';
    }

    public function isRequired() {
        $field_info = field_info_instance($this->entity_type, $this->field, $this->entity->type);
        if(!empty($field_info['required'])) return (bool)$field_info['required'];
        return false;
    }

    public function allowedValues() {
        $field = field_info_field($this->field);
        return list_allowed_values($field);
    }

    public function remove($column, $value) {
        if(is_array($this->get($column))) {
            $values = array_filter($this->get($column), function($var) use($value) {
                if($var != $value) return true;
                return false;
            });
            $values = array_values($values);
            $this->set($column, $values);
        } elseif($this->get($column) == $value) {
            $this->set($column, array());
        }
    }

    /**
     * @param $variables array('style_name' => 'thumbnail', 'alt' => '', 'title' => '');
     * @param int $index
     * @return string
     */
    public function renderImage($variables, $index=0) {
        $field_info = field_info_instance($this->entity_type, $this->field, $this->entity->type);
        $default_uri = '';
        if(!empty($field_info['settings']['default_image'])) {
            $file = file_load($field_info['settings']['default_image']);
            if($file)
                $default_uri = $file->uri;
        }
        // default_image_ft support
        if(!$default_uri) {
            if(!empty($field_info['settings']['default_image_ft_source_var'])) {
                $fid = variable_get($field_info['settings']['default_image_ft_source_var'], 0);
                if($fid && $file = file_load($fid)) {
                    $default_uri = $file->uri;
                }
            }
        }
        if(count($this->items) != 0 && !empty($this->items[$index]['uri'])) {
            $variables['path'] = $this->items[$index]['uri'];
        } else {
            $variables['path'] = $default_uri;
        }
        if($variables['path'])
            return theme('image_style', $variables);
        else
            return '';
    }
    
    public function printBacktrace() {
        $backtrace = array_slice(debug_backtrace(), 2, 5);
        foreach($backtrace as $key => $trace) {
            unset($backtrace[$key]['args']);
            unset($backtrace[$key]['object']);
        }
        watchdog('infinity', print_r($backtrace, true));
    }
}